package com.wps.wdztv6.controller.extend;

import com.wps.wdztv6.api.response.CreateFileResponse;
import com.wps.wdztv6.common.R;
import com.wps.wdztv6.config.AppServersProperties;
import com.wps.wdztv6.config.WdztAppInfoConfig;
import com.wps.wdztv6.entity.ThirdFile;
import com.wps.wdztv6.service.FileService;
import com.wps.wdztv6.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/extend/api/onLineFile")
public class CreateFileApiController {
    @Autowired
    private FileService fileService;

    @Autowired
    private UserInfoService userInfoService;


    @GetMapping("/create")
    public R<CreateFileResponse> getOnlineCooperators(
            @RequestParam("fileType") String fileType,
            @RequestHeader(value = WdztAppInfoConfig.USER_TOKEN_KEY, required = true) String token
    ) {
        userInfoService.saveUser(token);

        R<ThirdFile> result = fileService.createFileByTemplateFile(fileType);

        log.info("getOnlineCooperators:{}", result);

        CreateFileResponse data = new CreateFileResponse();
        data.setFileId(result.getData().getGroupFileId());
        data.setPreviewLink(String.format("%s/open/api/preview/link?file_id=%s", AppServersProperties.myserverUrl, result.getData().getGroupFileId()));
        data.setEditLink(String.format("%s/open/api/edit/link?file_id=%s", AppServersProperties.myserverUrl, result.getData().getGroupFileId()));

        log.info("在线编辑url: {}",
                String.format("%s/open/api/edit/link?file_id=%s", AppServersProperties.myserverUrl, result.getData().getGroupFileId()));
        log.info("在线预览 url: {}",
                String.format("%s/open/api/preview/link?file_id=%s", AppServersProperties.myserverUrl, result.getData().getGroupFileId()));


        return R.success("创建成功", data);

    }

}
